package com.hzc.springchan.service.impl;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.hzc.springchan.bean.request.FileRequest;
import com.hzc.springchan.bean.response.Response;
import com.hzc.springchan.constant.System;
import com.hzc.springchan.enums.PubError;
import com.hzc.springchan.service.IFOSService;
import com.hzc.springchan.service.IFileUploadService;
import com.hzc.springchan.service.ITokenService;
import com.hzc.springchan.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文件上传服务类
 * @author chen990627
 * @date 2022/10/19
 */
@Service
public class FileUploadServiceImpl implements IFileUploadService {

    @Autowired
    private ITokenService tokenService;

    @Autowired
    private IFOSService ossService;

    @Override
    public Response uploadFile(FileRequest request) {

        try {
            tokenService.getValue(request.getToken(), System.RequestParam.USER_ID, Integer.class, System.RequestParam.USER_ID);
        } catch (TokenExpiredException e) {
            LogUtils.error("token已失效： {}", e);
            return Response.newFailure(PubError.TOKEN_EXPIRE);
        } catch (Exception e) {
            LogUtils.error("token校验异常：{}", e);
            return Response.newFailure(PubError.INVALID_TOKEN);
        }
        return ossService.upload(request);
    }
}
